package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Audit;
import com.example.entity.Params;
import com.example.entity.User;
import com.example.mapper.AuditMapper;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuditService {
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;

    public List<Audit> findAll() {
        return auditMapper.selectAll();
    }


    public PageInfo<Audit> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());


        List<Audit> list = auditMapper.findBySearch(params);
        for (Audit audit : list) {
            if (ObjectUtil.isNotEmpty(audit.getUserid())) {
                User user = userMapper.selectById(audit.getUserid());
                if (ObjectUtil.isNotEmpty(user)) {
                    audit.setUserName(user.getName());
                }
            }
        }
        return PageInfo.of(list);
    }

    public void add(Audit audit) {
        auditMapper.add(audit);
    }

    public void update(Audit audit) {
        auditMapper.update(audit);
    }

    public void delete(Integer id) {

        auditMapper.deleteById(id);
    }


}
